/**
 * 指令查询
 * @author zhanghaitao
 */
import api from '../../api';
import moment from 'moment';
import { message } from 'antd';
import config from '../../comps/business/tradingMarket/config';
import typeConfig from '../../comps/business/tradingDirect/config';
import typeOfTradeX from '../../routes/PortfolioMnt/BookkeepingMnt/typeOfTradeX.js';
import findKeyword from '../../routes/PortfolioMnt/BookkeepingMnt/findKeyword';
import { fixedNum } from '../../utils/utils';
import statusData from '../../utils/enumeratedValues/statusData';
import delegationTypeData from '../../utils/enumeratedValues/delegationTypeData';
export default {
  namespace: 'queryTradeM',
  state: {},
  effects: {
    //获取指令查询列表
    *getList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.queryTrade.getList, payload);
      const columns = [
        {
          title: '指令下达时间',
          dataIndex: 'createdTs',
          key: 'createdTs',
          width: 200,
          render: (val, record) => (
            <span>
              {moment(val).format('YYYY-MM-DD')}-{record.createTime}
            </span>
          ),
        },
        {
          title: '产品名称',
          dataIndex: 'portfolioInfo',
          key: 'portfolioInfo',
          width: 120,
        },
        {
          title: '组合名称',
          dataIndex: 'portfolioInfoName',
          key: 'portfolioInfoName',
          width: 120,
          render: (val, record) => {
            return record.portfolioInfoCode + '-' + record.portfolioInfoName;
          },
        },
        {
          title: '证券名称',
          dataIndex: 'tradingName',
          key: 'tradingName',
          width: 150,
          render: (val, record) => {
            return record.tradingCode + '-' + record.tradingName;
          },
        },
        {
          title: '资产类型',
          dataIndex: 'type',
          key: 'type',
          width: 100,
          render: (val, record) => {
            let findType = typeConfig.filter((item) => {
              return item.value == record.type;
            });
            if (findType && findType.length > 0) {
              return findType[0].title;
            } else {
              return '--';
            }
          },
        },
        {
          title: '投资类型',
          dataIndex: 'investmentType',
          key: 'investmentType',
          width: 100,
        },
        {
          title: '委托方向',
          dataIndex: 'orderSide',
          key: 'orderSide',
          width: 80,
          render: (val, record) => {
            // 根据业务类型，翻译交易方向
            let trade = typeOfTradeX(record);
            if (trade) {
              let findtypes = typeConfig.filter((item) => {
                return item.value == record.type;
              });
              if (findtypes && findtypes.length > 0) {
                let findorderSide = findtypes[0].tradingDirect.filter((item) => {
                  return item.value == findKeyword(trade, record, 'orderSide');
                });
                if (findorderSide && findorderSide.length > 0) {
                  if (record.type == '3') {
                    //当业务类型为 3-协商成交，那么交易方向特殊处理成“方向-委托类型”
                    const delegationType = delegationTypeData.filter((item) => item.value == trade.delegationType);
                    return findorderSide[0].title + '-' + delegationType[0]?.title;
                  } else {
                    return findorderSide[0].title;
                  }
                } else {
                  return '--';
                }
              } else {
                return '--';
              }
            }
          },
        },
        {
          title: '交易市场',
          dataIndex: 'securityExchange',
          key: 'securityExchange',
          width: 100,
          render: (val, record) => {
            let trade = typeOfTradeX(record);
            if (trade) {
              let findvalue = config.filter((item) => {
                if (record.type == '19') {
                  // 港股通
                  return item.value == trade.exchangecode;
                }
                return item.value == trade.exchangeCode;
              });
              if (findvalue && findvalue.length > 0) {
                return findvalue[0].title;
              } else {
                return '--';
              }
            }
          },
        },
        {
          title: '指令数量',
          dataIndex: 'tAmount',
          key: 'tAmount',
          type: 'number',
          width: 120,
          render: (val, record) => {
            let trade = typeOfTradeX(record);
            return findKeyword(trade, record, 'tAmount');
          },
        },
        {
          title: '价格',
          dataIndex: 'price',
          key: 'price',
          type: 'money',
          width: 120,
          render: (val, record) => {
            let trade = typeOfTradeX(record);
            if (record.type == '7') {
              return '100';
            }
            if (record.type == '9') {
              if (trade.type == 1) {
                return '--';
              } else {
                return findKeyword(trade, record, 'price', 'moneys');
              }
            }
            if (record.type == '19') {
              // 港股通
              let text = findKeyword(trade, record, 'price', 'moneys');
              if (text == '--') {
                return '--';
              } else {
                return text + '(HKD)';
              }
            }
            return findKeyword(trade, record, 'price', 'moneys');
          },
        },
        {
          title: '指令金额',
          dataIndex: 'settlementAmount',
          key: 'settlementAmount',
          type: 'money',
          className: 'column-right',
          width: 120,
          render: (val, record) => {
            let trade = typeOfTradeX(record);
            if (record.type == '9') {
              if (trade.type == 1) {
                return '--';
              } else {
                return findKeyword(trade, record, 'settlementAmount', 'moneys');
              }
            }
            if (record.type == '19') {
              // 港股通
              let text = findKeyword(trade, record, 'settlementAmount', 'moneys');
              if (text == '--') {
                return '--';
              } else {
                return text + '(HKD)';
              }
            }
            if (findKeyword(trade, record, 'settlementAmount', 'moneys')) {
              return findKeyword(trade, record, 'settlementAmount', 'moneys');
            } else {
              return fixedNum(val);
            }
          },
        },
        {
          title: '指令状态',
          dataIndex: 'busStatus',
          key: 'busStatus',
          width: 90,
          render: (val, record) => {
            let findStatus = statusData.busStatus.filter((item) => {
              return item.value == record.busStatus;
            });
            if (findStatus && findStatus.length > 0) {
              return findStatus[0].title;
            } else {
              return '--';
            }
          },
        },
        {
          title: '成交状态',
          dataIndex: 'clinchStatus',
          key: 'clinchStatus',
          width: 90,
          render: (val, record) => {
            let findStatus = statusData.clinchStatus.filter((item) => {
              return item.value == record.clinchStatus;
            });
            if (findStatus && findStatus.length > 0) {
              return findStatus[0].title;
            } else {
              return '--';
            }
          },
        },
        {
          title: '支付状态',
          dataIndex: 'payStatus',
          key: 'payStatus',
          width: 90,
          render: (val, record) => {
            let findStatus = statusData.payStatus.filter((item) => {
              return item.value == record.payStatus;
            });
            if (findStatus && findStatus.length > 0) {
              return findStatus[0].title;
            } else {
              return '--';
            }
          },
        },
        {
          title: '创建人',
          dataIndex: 'userName',
          key: 'userName',
          width: 120,
        },
      ];

      if (res && res.success) {
        res.data &&
          res.data.map((item) => {
            if (item.type == '17') {
              //中签状态 拿到外层 方便使用tableC操作按钮
              item.bondOffStatus = item.trade17.status;
            }
          });
        callback({
          pageInfo: {
            total: res.total,
            currentPage: res.current,
            pageSize: res.pageSize,
          },
          dataSource: res.data,
          columns: columns,
        });
      }
    },
    //撤单
    *recallTrade({ payload, callback }, { call, put }) {
      const response = yield call(api.queryTrade.recallTrade, payload);
      if (response && response.success) {
        callback(true);
        message.success('撤单成功');
      } else {
        callback();
        message.error(response.message || '撤单失败!');
      }
    },

    //风控信息
    *riskInfromation({ payload, callback }, { call, put }) {
      const response = yield call(api.queryTrade.riskInfromation, payload);
      const columns = [
        {
          title: '限额信息',
          dataIndex: 'ruleDesc',
          key: 'ruleDesc',
          width: 240,
        },
        {
          title: '触警级别',
          dataIndex: 'ruleWarnType',
          key: 'ruleWarnType',
        },
        {
          title: '触警限额',
          dataIndex: 'alarmLimit',
          key: 'alarmLimit',
        },
        {
          title: '禁止限额',
          dataIndex: 'prohibitedLimit',
          key: 'prohibitedLimit',
        },
        {
          title: '备注',
          dataIndex: 'remark',
          key: 'remark',
        },
      ];
      if (response && response.code == 0) {
        callback(response.data.dataSource, columns);
      } else {
        callback();
        message.error(response?.msg || '风控信息查询失败!');
      }
    },
    //成交信息
    *clinchInfromation({ payload, callback }, { call, put }) {
      const response = yield call(api.queryTrade.clinchInfromation, payload);
      if (response && response.code == 0) {
        callback(response.data);
      } else {
        callback();
        message.error(response?.message || '成交信息查询失败!');
      }
    },
  },
  reducers: {
    updateData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  subscriptions: {},
};
